package com.example.noobpackage.ui.packagemarket

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.noobpackage.R
import com.example.noobpackage.logic.model.MarketPackageData
import com.example.noobpackage.ui.MainViewModel
import com.example.noobpackage.ui.login.LoginFragment
import com.example.noobpackage.ui.util.LoginRequiredFragment
import kotlinx.android.synthetic.main.fragment_package_market.*
import kotlinx.android.synthetic.main.fragment_package_market.swipeRefresh
import kotlinx.android.synthetic.main.fragment_proxy_package.*

class PackageMarketFragment : LoginRequiredFragment() {
    private val sharedViewModel: MainViewModel by activityViewModels()
    private val viewModel: PackageMarketViewModel by viewModels()
    private val marketList = ArrayList<MarketPackageData>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        setupReturn()
        return inflater.inflate(R.layout.fragment_package_market, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        Log.d("packageMarket", "onViewCreated")
        super.onViewCreated(view, savedInstanceState)
        loginJudge { user ->

            setupMarketList()

            // 绑定 刷新事件
            swipeRefresh.setColorSchemeResources(R.color.green_500)
            swipeRefresh.setOnRefreshListener {
                refreshList(user.account)
            }
            // 初次更新数据
            swipeRefresh.isRefreshing = true

            refreshList(user.account)
        }
    }

    private fun setupMarketList() {

        //建立View和数据的关联 MyApplication.context
        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        packagesListView.layoutManager = layoutManager
        val listAdapter = PackageMarketAdapter()
        packagesListView.adapter = listAdapter

        // 让过渡动画延迟到列表加载完成后生效
        packagesListView.apply {
            this.layoutManager = layoutManager
            this.adapter = listAdapter
            postponeEnterTransition()
            viewTreeObserver.addOnPreDrawListener {
                startPostponedEnterTransition()
                true
            }
        }

        listAdapter.setItemClick { position, extras ->
            val action = PackageMarketFragmentDirections.packageMarketDetail(position)
            findNavController().navigate(action, extras)
        }

        // 设置list数据更新
        viewModel.marketList.observe(viewLifecycleOwner, Observer {
            listAdapter.setMarketList(it)
            swipeRefresh.isRefreshing = false
        })
    }

    private fun refreshList(account: Long) {
        Log.d("packageMarket", "refreshList")
        viewModel.updateMarketList(account)
    }
}